var sliderWidth = 47; // 需要设置slider的宽度，用于计算中间位置
const app = getApp();
Page({
  data: {
    tabs: ["最新简历", "未通知", "已通知"],
    activeIndex: 0,
    sliderOffset: 0,
    sliderLeft: 0,

    assoId:null,
    page:0,
    list:[],
    hasMoreData:true,
    msg:"正在加载",
    total:0
  },
  onLoad: function () {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          sliderLeft: (res.windowWidth / that.data.tabs.length - sliderWidth) / 2,
          sliderOffset: res.windowWidth / that.data.tabs.length * that.data.activeIndex
        });
      }
    });
  },
  tabClick: function (e) {
    this.setData({
      sliderOffset: e.currentTarget.offsetLeft,
      activeIndex: e.currentTarget.id
    });
    var that =  this
    that.onShow();
  },
  getAssoId:function(){
    var that = this
    var code = that.data.activeIndex;
    wx.request({
      url: app.globalData.url + '/asso/getAssoId',
      data: {
        schoolName: app.globalData.studentInfo.schoolName,
        uniqueId: app.globalData.uniqueId,
      },
      success: res => {
        that.setData({
          assoId: res.data
        })
        wx.request({
          url: app.globalData.url + '/vitae/checkVitae',
          data: {
            code: code,
            assoId: that.data.assoId,
            page:that.data.page
          },
          success: res => {
            that.setData({
              list: res.data.checkVitaeVOList,
              hasMoreData: res.data.count >= that.page,
              total:res.data.total
            })
            wx.showToast({
              title: '显示成功'
            });
          },
          fail: function fail(e) {
            wx.showToast({
              title: "获取数据失败",
              icon: 'none',
              duration: 2000 //弹出时间	
            });
          },
          complete: function complete(e) {
            wx.hideLoading();
            wx.hideNavigationBarLoading();
            wx.stopPullDownRefresh();
          }
        })
      }
    })    
  },
  detail: function (e) {
    var that = this;
    var index = parseInt(e.currentTarget.dataset.index);
    var vitaeId = this.data.list[index].vitaeId
    var deliveryId = this.data.list[index].deliveryId
    wx.request({
      url: app.globalData.url +'/vitae/changeDeliveryCode',
      data:{
        id: deliveryId,
        code:1
      },
      success:res=>{
        wx.navigateTo({
          url: '../vitae/vitae?id=' + vitaeId + '&isPreview=2&deliveryId=' + deliveryId,
        })
      }
    })
    console.log(index)
  },
  onShow:function(){
    var that = this
    wx.showLoading({
      title: that.data.msg
    });
    that.getAssoId();  
  },
  onPullDownRefresh: function () {
    var that = this
    wx.showNavigationBarLoading();
    that.data.page = 0;
    that.setData({
      msg: "正在刷新数据"
    });
    that.onShow();

  },
  onReachBottom: function () {
    var that = this;
    if (that.data.hasMoreData) {
      that.setData({
        page: that.data.page + 1,
        msg: "加载更多数据"
      });
      that.onShow();
    } else {
      wx.showToast({
        title: '没有更多数据'
      });
    }
  },
  option: function (e) {
    var that = this
    var index = parseInt(e.currentTarget.dataset.index);
    var id = this.data.list[index].id
    wx.showActionSheet({
      itemList: [''],
      success: function (res) {
        if (!res.cancel) {
          wx.request({
            url: app.globalData.url + '/vitae/deleteVitae',
            data: {
              id: id,
            },
            success: res => {
              if (res.data.code == 0) {
                wx.showModal({
                  title: '提醒',
                  content: res.data.msg,
                })
              } else {
                that.onShow();
              }
            }
          })
        }
      }
    });
  },
});